package com.xclink.sys.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.xclink.common.utils.ValidateCodeUtils;
import com.xclink.sys.entity.User;
import com.xclink.sys.service.UserService;
@Controller
public class LogController {
	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(){
		return "login";
	}
	
//	登录校验
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String dologin(User user,Model model,	HttpServletRequest request){
		HttpSession session =request.getSession();
		String validateCode = (String) session.getAttribute("validateCode");
		String validateCodeFromPage = request.getParameter("validateCode");
		if(validateCodeFromPage!=null && validateCode!=null && !validateCodeFromPage.equalsIgnoreCase(validateCode)){
			model.addAttribute("message", "验证码错误");
			return "login";
		}
		User sUser=userService.checkLogin(user.getUsername(), user.getPassword());
		if(sUser==null){
			model.addAttribute("message", "账号或密码错误");
			return "login";
		}
		if(!sUser.getUserType().equals("0") && !sUser.getUserType().equals("1")){
			model.addAttribute("message", "暂无权限");
			return "login";
		}
		session.setAttribute("currentUser",sUser);
//		model.addAttribute("currentUser",sUser);
		return "redirect:index";
	}
//验证码生成
	@RequestMapping("/validateCode")
	public void validateCode(HttpServletRequest request, HttpServletResponse response) throws IOException{
		response.setHeader("Pragma", "No-cache");  
		response.setHeader("Cache-Control", "no-cache");  
		response.setDateHeader("Expires", 0);  
		response.setContentType("image/jpeg");  
		// 生成随机字串
		String verifyCode = ValidateCodeUtils.generateVerifyCode(4);  
		// 存入会话session 
		HttpSession session = request.getSession(true);  
		session.setAttribute("validateCode", verifyCode.toLowerCase());  
		// 生成图片
		int w = 200, h = 80;  
		ValidateCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);  
	}
//	首页
	@RequestMapping("/index")
	public String index(HttpSession session,Model model){
		User user =(User) session.getAttribute("currentUser");
//		获取最新的信息，更新session
		user = userService.get(user.getUserId());
		session.setAttribute("currentUser",user);
		model.addAttribute("currentUser", user);
		return "index";
	}
//	欢迎页面
	@RequestMapping("/welcome")
	public String welcome(){
		return "welcome";
	}
	 //注销方法
    @RequestMapping("/outLogin")
    public String outLogin(HttpSession session){
        //通过session.invalidata()方法来注销当前的session
        session.invalidate();
        return "login";
    }
    
}
